package com.arceus.leetcode.dytedance;

import java.util.Stack;

/**
 * @author : iron
 * @version : 1.0.0
 * @date : 4:13 PM 2018/12/27
 */

public class T6 {
    public static void main(String[] args) {

        String path = "/a/./b///../c/../././../d/..//../e/./f/./g/././//.//h///././/..///";
        System.out.println(simplifyPath(path));
    }

    public static String simplifyPath(String path) {

        Stack<String> stack = new Stack<>();
        path = path.replace("//", "/");
        String[] p = path.split("/");
        for (int i = 1; i < p.length; i++) {

            if ("..".equals(p[i])) {
                if (stack.size() != 0) {
                    stack.pop();
                }
                continue;
            }
            if (!".".equals(p[i])&&!"".equals(p[i])) {
                stack.push(p[i]);
            }
        }

        String res = "/";
        if (stack.size()==0){
            return "/";
        }
        for (String s : stack) {
            if (!"".equals(s)) {
                res += s + "/";
            }
        }
        return res.substring(0, res.length() - 1);
    }
}
